﻿using Bandit.Helpers;
using Bandit.UI.MVVM;
using ETC.DLEDPositionTool.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ETC.DLEDPositionTool.Windows
{
    /// <summary>
    /// FrmManage.xaml 的交互逻辑
    /// </summary>
    public partial class FrmSetting : Bandit.UI.BanditWindow
    {
        /// <summary>
        /// 视图模型
        /// </summary>
        private ViewModels.SettingViewModel ViewModel { get { return App.SettingViewModel; } }

        public FrmSetting()
        {
            InitializeComponent();

            this.Loaded += delegate
            {
                this.ViewModel.OnClick = new Bandit.UI.MVVM.CommandHandler(OnClick);

                this.DataContext = ViewModel;
            };

            this.Closed += (sender, e) =>
            {
                // 当窗口关闭后马上结束消息循环
                // System.Windows.Threading.Dispatcher.ExitAllFrames();
            };
        }

        #region UI Click Event

        private void OnClick(object parameter)
        {
            switch (parameter.ToString())
            {
                case "Close":
                    {
                        this.Close();
                    }
                    break;
                case "SaveSetting":
                    {
                        ConfigHelper.XmlFileSerialize<Setting>(this.ViewModel.Setting, Setting.SavePath);

                        Task.Factory.StartNew(() =>
                        {
                            System.Threading.Thread.Sleep(1000);
                        });

                        this.ShowDialog(App.GetString("SaveSuccess"), App.GetString("Prompt"), MessageBoxButton.OK, Bandit.UI.DialogIcon.OK);

                        this.Close();
                    }
                    break;
                default:
                    break;
            }
        }

        #endregion
    }
}
